package com.github.postsanf.yinian.slider.library.Tricks;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.flaviofaria.kenburnsview.KenBurnsView;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;

import java.util.List;

/**
 * Created by Administrator on 2016/8/12.
 */
public class ImageAdapter extends PagerAdapter {
    private List<String> viewlist;
    private Context mContext;
    private ImageLoader mImageLoader;

    public ImageAdapter(Context mContext, List<String> viewlist) {
        this.mContext = mContext;
        this.viewlist = viewlist;
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext)
                .memoryCacheExtraOptions(480, 800)
                .diskCacheExtraOptions(480, 800, null)
                .threadPoolSize(5)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new UsingFreqLimitedMemoryCache(10 * 1024 * 1024))
                .memoryCacheSize(10 * 1024 * 1024)
                .diskCacheSize(50 * 1024 * 1024)
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .defaultDisplayImageOptions(ImageOptHelper.getImgOptions())
                .diskCacheFileCount(150)
                .imageDownloader(new BaseImageDownloader(mContext, 5 * 1000, 30 * 1000))
                .build();
        ImageLoader.getInstance().init(config);
        this.mImageLoader = ImageLoader.getInstance();
    }

    @Override
    public int getCount() {
        return viewlist.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position,
                            Object object) {
        //Warning：不要在这里调用removeView
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        KenBurnsView mKenBurnsView = new KenBurnsView(mContext);
        mKenBurnsView.setLayoutParams(new ViewPager.LayoutParams());
        String url= YNUrlHelper.getCoverUrl(mContext,viewlist.get(position));
        mImageLoader.displayImage(url, mKenBurnsView);
        container.addView(mKenBurnsView);
        return mKenBurnsView;
    }
}
